package com.andon.microcellsimulator.common.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 该类为辅助ListViewAdapter的复用模型，参考如下代码
 public View getView(int position, View convertView, ViewGroup parent)
 {
 //实例化一个viewHolder
 ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent,
 R.layout.item_single_str, position);
 //通过getView获取控件
 TextView tv = viewHolder.getView(R.id.id_tv_title);
 //使用
 tv.setText(mDatas.get(position));
 go_back_white viewHolder.getConvertView();
 }
 *
 */
public class ViewHolder
{
    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position)
    {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        // setTag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position)
    {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder(context, parent, layoutId, position);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.mPosition = position;
        }
        return holder;
    }

    public View getConvertView()
    {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId)
    {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 设置View的显隐
     *
     * @param viewId
     * @param visibility
     * @return
     */
    public ViewHolder setVisibility(int viewId, int visibility)
    {
        View view = getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text)
    {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId)
    {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param bm
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm)
    {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为TextView设置字体颜色
     *
     * @param viewId
     * @param colorValue
     * @return
     */
    public ViewHolder setTextColor(int viewId, int colorValue)
    {
        TextView view = getView(viewId);
        view.setTextColor(colorValue);

        return this;
    }

    /**
     * 设置View的点击事件
     *
     * @param viewId
     * @param onClickListener
     * @return
     */
    public ViewHolder setOnClick(int viewId, View.OnClickListener onClickListener)
    {
        View view = getView(viewId);
        view.setOnClickListener(onClickListener);
        return this;
    }

    public int getPosition()
    {
        return mPosition;
    }

}
